package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.mongo.VideosApi;
import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.mongo.Focus;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@DubboService
public class VideosApiImpl implements VideosApi {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    /**
     * 发布小视频
     *
     * @param video
     */
    @Override
    public void saveVideo(Video video) {
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        mongoTemplate.insert(video);
    }

    /**
     * 获取小视频列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<Video> findVideoList(Long page, Long pagesize) {
        Query query = new Query().with(Sort.by(Sort.Order.desc("likeCount")));
        long count = mongoTemplate.count(query, Video.class);
        query.skip((page - 1) * pagesize).limit(pagesize.intValue());
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        return new PageResult<>(page, pagesize, count, videoList);
    }

    /**
     * 关注用户
     *
     * @param focus
     * @param userId
     */
    @Override
    public void focusUser(Focus focus, Long userId) {
        long timeMillis = System.currentTimeMillis();
        Query query = new Query(Criteria.where("userId").is(userId).and("focusUserId").is(focus.getFocusUserId()));
        if (mongoTemplate.exists(query, Focus.class)) {
            Update update = new Update();
            update.set("isFocus", true);
            update.set("updated", timeMillis);
            mongoTemplate.updateFirst(query, update, Focus.class);
        } else {
            focus.setCreated(timeMillis);
            focus.setUpdated(timeMillis);
            mongoTemplate.insert(focus);
        }
    }

    /**
     * 视频用户关注 - 取消
     *
     * @param uid
     * @param userId
     */
    @Override
    public void userUnFocus(Long uid, Long userId) {
        long timeMillis = System.currentTimeMillis();
        Query query = new Query(Criteria.where("userId").is(userId).and("focusUserId").is(uid));
        if (mongoTemplate.exists(query, Focus.class)) {
            Update update = new Update();
            update.set("isFocus", false);
            update.set("updated", timeMillis);
            mongoTemplate.updateFirst(query, update, Focus.class);
        }
    }

    /**
     * 视频点赞
     *
     * @param id
     */
    @Override
    public void likeVideo(String id, Long userId) {
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update();
        mongoTemplate.updateFirst(query, update.inc("likeCount", 1), Video.class);
    }

    /**
     * 视频点赞 - 取消
     *
     * @param id
     * @return
     */
    @Override
    public void disLike(String id, Long userId) {
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update();
        mongoTemplate.updateFirst(query, update.inc("likeCount", -1), Video.class);
    }

    @Override
    public PageResult<Video> getRecommend(Long page, Long pagesize, String[] vid) {
        Query query = new Query(Criteria.where("vid").in(Arrays.stream(vid).toArray()));
        long count = vid.length;
        long start = (page - 1) * pagesize;
        if (count > start){
            query.skip(start).limit(pagesize.intValue());
        }
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        return new PageResult<>(page,pagesize,count,videoList);
    }

}
